/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cart.hibernate.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author BibooChung
 */
public class ManageOrderServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("submit");
        try {
            ManageOrder manager = new ManageOrder();

            Date orderDatetime; 
            Date shippedDatetime; 
            String creditCardNum; 
            long paymentMethodId; 
            int orderStatusId; 
            String flat; 
            String blockAndBuilding; 
            String district;
            String region; 
            String loginName; 
            List movieId = new ArrayList(); 
            List quantityList = new ArrayList();

            long newOrderId;
            long orderId;

            
            boolean isDeleted = true;

                out.println("<h1>66</h1>");
            if("add".equals(action) || "update".equals(action)){

                
                out.println("<h1>69</h1>");
                orderDatetime = new SimpleDateFormat("dd-mm-yyyy").parse(request.getParameter("orderDatetime"));       
                shippedDatetime = new SimpleDateFormat("dd-mm-yyyy").parse(request.getParameter("shippedDatetime"));
                creditCardNum = request.getParameter("creditCardNum");
                paymentMethodId = Long.parseLong(request.getParameter("paymentMethodId"));
                orderStatusId = Integer.parseInt(request.getParameter("orderStatusId"));
                flat = request.getParameter("flat");
                blockAndBuilding = request.getParameter("String blockAndBuilding");
                district = request.getParameter("district");
                region = request.getParameter("region");
                loginName = request.getParameter("loginName");
               
                out.println("<h1>82</h1>");
          //      movieId = Arrays.asList(request.getParameterValues("movieId"));
          //      quantityList = Arrays.asList(request.getParameterValues("quantityList"));
//--------------------------------------------------------------following not yet done
                
                out.println("<h1>87</h1>");
                if ("add".equals(action))
                {
                    newOrderId = manager.addOrder( orderDatetime, shippedDatetime, 
                                             creditCardNum, paymentMethodId, 
                                             orderStatusId, flat, blockAndBuilding, district,
                                             region, loginName, movieId, quantityList);
                
                out.println("<h1>95</h1>");
                }
                else
                {
                    
                out.println("<h1>100</h1>");
                    orderId = Long.parseLong(request.getParameter("orderId"));
                    manager.updateOrder( orderId, orderDatetime, shippedDatetime, 
                                             creditCardNum, paymentMethodId, 
                                             orderStatusId, flat, blockAndBuilding, district,
                                             region, loginName, movieId, quantityList);
                }
            }
            else if("delete".equals(action))
            {
                manager.deleteOrder(Long.parseLong(request.getParameter("orderId")));
               
            }
            else
                throw new IOException();
    

            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ManageMemberServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            /*if (isDeleted)*/
                out.println("<h1>Success</h1>");
            /*else
            {
                out.println("<h1>Fail to delete member --> '" + request.getParameter("loginName") + "'</h1>");
                out.println("<p>This member has already ordered some items, it is impossible to delete this member.</p>");
            }*/
            out.println("</body>");
            out.println("</html>");
       /* } catch (IOException e) {
          System.err.println("Form submit value is changed\nError stack trace :\n");
          e.printStackTrace();*/
        } catch (ParseException e){
            out.println("<h1>Date parsing error</h1>");
          System.err.println("Date parsing error");
          e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
